package com.admin.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.model.ShowBak;
import com.bayss.core.service.BakService;
import com.bayss.core.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户操作
 *
 * @author yangjian
 */
@Slf4j
@RestController
public class FileController extends ApiBaseController{
    private OSS ossClient;
    private final BakService bakService;

    @Autowired
    public FileController(BakService bakService){
        ossClient = new OSSClientBuilder().build("http://oss-cn-beijing.aliyuncs.com", "LTAI4FjvBA5HR1Yk4xf6AERV", "oKL3jHhk4pX4nL8rZrvHuNPSkyhC0C");
        this.bakService = bakService;
    }

    /**
     * 图片上传
     *
     * @return success
     */
    @RequestMapping("/upload.json")
    public ResultMsg upload(@RequestParam(name = "doko_admin_file") MultipartFile file) throws Exception {
        String name = System.currentTimeMillis()+DigestUtils.md5Hex(file.getOriginalFilename()).toUpperCase();
        name = "doko-admin/"+name;
        PutObjectRequest putObjectRequest = new PutObjectRequest("doko-way", name, file.getInputStream());
        ossClient.putObject(putObjectRequest);
        bakService.saveBak(name, "doko-admin");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", name);
        jsonObject.put("url", FileUtil.imgUrl(name));
        return ResultMsg.success(jsonObject);
    }

    /**
     * 图片需要备份接口
     *
     * @return success
     */
    @RequestMapping("/get_baks.json")
    public ResultMsg getBaks(@RequestParam(name = "maxId", required = false, defaultValue = "0") Integer maxId,
                             @RequestParam(name = "count", required = false, defaultValue = "10") Integer count) {
        List<ShowBak> showBaks = bakService.getList(maxId, count);
        JSONObject jsonObject = new JSONObject();
        if (showBaks.size() == 0) {
            jsonObject.put("maxId", -1);
            jsonObject.put("baks", showBaks);
        } else {
            jsonObject.put("maxId", showBaks.get(showBaks.size()-1).getId());
            jsonObject.put("baks", showBaks);
        }
        return ResultMsg.success(jsonObject);
    }

    /**
     * 图片备份完成上报
     *
     * @return success
     */
    @RequestMapping("/submit_baks.json")
    public ResultMsg submit(@RequestParam(name = "ids") String ids) {
        String[] idList = ids.split(",");
        for (String id : idList) {
            bakService.submitBak(Integer.parseInt(id));
        }
        return ResultMsg.success(true);
    }

}
